package com.fatgirl.utils.binding
{
	import flash.events.Event;
	import flash.utils.Dictionary;

	/**
	 * 绑定类 
	 * @author iceman
	 * 
	 */
	public class DataBinding
	{
		private static var m_siteList:Dictionary = new Dictionary(false);
		public function DataBinding()
		{
		}
		public static function binding(_site:Object,_pro:String,_metaData:MetaData,_metaDataPro:String):void
		{
			if(!_site || !_site.hasOwnProperty(_pro)) throw Error("被绑定对象或者对象属性不存在：_site="+_site+",_pro="+_pro);
			if(!_metaData || !_metaData.hasOwnProperty(_metaDataPro)) throw new Error("元数据或元数据属性不存在：_metaData="+_metaDataPro+",_metaDataPro="+_metaDataPro);
			var t_site:SiteObj = m_siteList[_site] as SiteObj;
			var t_arr:Array;
			if(!t_site)
			{
				t_site = new SiteObj;
				t_arr = t_site.proList;
				t_site.obj = _site;
			}
			else
			{
				for(var i:int = 0 ;i<t_arr.length;i++) if(t_arr[i]==_pro){throw Error("不能重复添加绑定："+_site+","+_pro)};
				m_siteList[_site] = t_site;
			}
			t_arr[_metaDataPro] = _pro;
			if(_metaData[_metaDataPro]!=null)
			{
				var t_b:Boolean = _site[_pro] is Function;
				if(t_b) (_site[_pro] as Function).call(null,_metaData[_metaDataPro]) else _site[_pro] = _metaData[_metaDataPro];
			}
			_metaData.addEventListener(_metaDataPro,t_site.callBack);
			//trace("添加绑定成功：_site="+_site+" ,_pro="+_pro+" ,_metaData="+_metaData+" ,_metaDataPro="+_metaDataPro);
		}
		public static function removeBingding(_site:Object,_pro:String,_metaData:MetaData,_metaDataPro:String):void
		{
			var t_site:SiteObj = m_siteList[_site] as SiteObj;
			if(t_site)
			{
				for(var i:int = 0;i<t_site.proList.length;i++) if(_pro == t_site.proList[i]) {t_site.proList.splice(i,1);break;}
				if(_metaData && !t_site.proList.length)_metaData.removeEventListener(_metaDataPro,t_site.callBack);
			}
		}
	}
}